package com.yupi.moshu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.moshu.common.BaseResponse;
import com.yupi.moshu.common.ErrorCode;
import com.yupi.moshu.common.ResultUtils;
import com.yupi.moshu.exception.BusinessException;
import com.yupi.moshu.mapper.ProjectMapper;
import com.yupi.moshu.model.domain.Project;
import com.yupi.moshu.model.domain.Team;
import com.yupi.moshu.model.domain.TeamUser;
import com.yupi.moshu.model.domain.User;
import com.yupi.moshu.model.request.*;
import com.yupi.moshu.service.ProjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目管理接口
 * @author 7
 */
@RestController
@RequestMapping("/project")
public class ProjectController {
    @Resource
    private ProjectService projectService;

    @Resource
    private ProjectMapper projectMapper;

    @PostMapping(value = "/createProject")
    public BaseResponse<Void> createProject(@RequestBody CreateProjectRequest createProjectRequest) {
        Long adminID = createProjectRequest.getAdminID();
        Long teamID = createProjectRequest.getTeamID();
        String projectName = createProjectRequest.getProjectName();
        String description = createProjectRequest.getDescription();
        Date deadline = createProjectRequest.getDeadline();

        if (StringUtils.isAnyBlank(projectName, description)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "部分参数为空");
        }

        Project project = new Project();
        project.setName(projectName);
        project.setAdminID(adminID);    // 创建者
        project.setTeamID(teamID);      // 项目所属团队
        project.setDescription(description);    //项目描述
        project.setDeadline(deadline);
        projectService.save(project);
        return ResultUtils.success("项目创建成功");
    }

    @PostMapping(value = "/deleteProject")//删除项目
    public BaseResponse<Void> deleteProject(@RequestBody DeleteProjectRequest deleteProjectRequest) {
        Long projectID = deleteProjectRequest.getProjectID();
        projectService.removeById(projectID);
        return ResultUtils.success("成功删除项目");
    }

    @PostMapping(value = "/renameProject")//重命名项目
    public BaseResponse<Void> renameProject(@RequestBody RenameProjectRequest renameProjectRequest) {
        Long projectID = renameProjectRequest.getProjectID();
        String nameNeo=renameProjectRequest.getNameNeo();
        projectMapper.rename(nameNeo,projectID);
        return ResultUtils.success("成功重命名项目");
    }

    @PostMapping(value = "/fullDeleteProject")//彻底删除
    public BaseResponse<Void> fullDeleteProject(@RequestBody FullDeleteProjectRequest fullDeleteProjectRequest) {
        Long projectID = fullDeleteProjectRequest.getProjectID();
        projectMapper.fullKill(projectID);
        return ResultUtils.success("成功彻底删除项目");
    }

    @PostMapping(value = "/recycleBin") //查看回收站
    public BaseResponse<List<Project>> recycleBin(@RequestBody RecycleBinRequest recycleBinRequest) {
        Long teamID = recycleBinRequest.getTeamID();
        List<Project> projectList = projectMapper.recycleBin(teamID);
        return ResultUtils.success(projectList);
    }

    @PostMapping(value = "/normalBin") //正常列表
    public BaseResponse<List<Project>> normal(@RequestBody NormalBinRequest normalBinRequest) {
        Long teamID = normalBinRequest.getTeamID();
        int sortWay = normalBinRequest.getSortWay();
        List<Project> projectList = null;
        switch (sortWay) {
            case 0:
                projectList = projectMapper.normalBin(teamID);
                break;
            case 1:
                projectList = projectMapper.normalBin1(teamID);
                break;
            case 2:
                projectList = projectMapper.normalBin2(teamID);
                break;
            case 3:
                projectList = projectMapper.normalBin3(teamID);
                break;
        }
        return ResultUtils.success(projectList);
    }

    @PostMapping(value = "/restore") //恢复已删除的项目
    public BaseResponse<Void> restore(@RequestBody RestoreRequest restoreRequest) {
        Long projectID = restoreRequest.getProjectID();
        projectMapper.restore(projectID);
        return ResultUtils.success("已恢复该项目");
    }

    @PostMapping(value = "/searchProject")//搜索项目
    public BaseResponse<List<Project>> searchProject(@RequestBody SearchProjectRequest searchProjectRequest) {
        String name = searchProjectRequest.getName();
        Long teamID = searchProjectRequest.getTeamID();
        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("teamID", teamID).like("name", name);
        List<Project> projectList = projectService.list(projectQueryWrapper);
        return ResultUtils.success(projectList);
    }

    @PostMapping(value = "/copyProject")//复制项目
    public BaseResponse<Void> copyProject(@RequestBody CopyProjectRequest copyProjectRequest) {
        Long ID = copyProjectRequest.getId();
        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("id", ID);
        Project project = projectService.getOne(projectQueryWrapper);

        String nameNeo=project.getName();
        QueryWrapper<Project> projectQueryWrapper1 = new QueryWrapper<>();
        projectQueryWrapper1.like("name",nameNeo);
        List<Project> projectList=projectService.list(projectQueryWrapper1);
        long num= (long) projectList.size();


        Project project1 = new Project();
        project1.setName(project.getName()+"_"+num);
        project1.setAdminID(project.getAdminID());    // 创建者
        project1.setTeamID(project.getTeamID());      // 项目所属团队
        project1.setDescription(project.getDescription());    //项目描述
        project1.setDeadline(project.getDeadline());
        projectService.save(project1);

        return ResultUtils.success("复制成功");
    }

}
